<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>对浮点数进行四舍五入</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.rand.html">rand</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.sin.html">sin</a></div>
 <div class="up"><a href="ref.math.html">Math 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.round" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">round</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">round</span> &mdash; <span class="dc-title">对浮点数进行四舍五入</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.round-description">
  <h3 class="title">说明</h3>
   <div class="methodsynopsis dc-description">
    <span class="type">float</span> <span class="methodname"><strong>round</strong></span>
     ( <span class="methodparam"><span class="type">float</span> <code class="parameter">$val</code></span>
    [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$precision</code><span class="initializer"> = 0</span></span>
    [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = PHP_ROUND_HALF_UP</span></span>
   ]] )</div>

    <p class="para rdfs-comment">
     返回将 <em><code class="parameter">val</code></em> 根据指定精度
     <em><code class="parameter">precision</code></em>（十进制小数点后数字的数目）进行四舍五入的结果。<em><code class="parameter">precision</code></em>
     也可以是负数或零（默认值）。
    </p>
    
    <p class="para">
     <blockquote class="note"><p><strong class="note">Note</strong>: 
      <span class="simpara">
       PHP 默认不能正确处理类似 <em>&quot;12,300.2&quot;</em>
       的字符串。见<a href="language.types.string.html#language.types.string.conversion" class="link">字符串转换为数值</a>。
      </span>
     </p></blockquote>
    </p>
   </div>

   <div class="refsect1 parameters" id="refsect1-function.round-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">val</code></em></dt>

     <dd>

      <p class="para">
       要处理的值
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">precision</code></em></dt>

     <dd>

      <p class="para">
       可选的十进制小数点后数字的数目。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">mode</code></em></dt>

     <dd>

      <p class="para">
       以下之一： <strong><code>PHP_ROUND_HALF_UP</code></strong>、
       <strong><code>PHP_ROUND_HALF_DOWN</code></strong>
       <strong><code>PHP_ROUND_HALF_EVEN</code></strong> 或
       <strong><code>PHP_ROUND_HALF_ODD</code></strong>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.round-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   四舍五入后的值
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.round-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3382">
    <p><strong>Example #1 <span class="function"><strong>round()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">3.4</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;3<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">3.5</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;4<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">3.6</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;4<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">3.6</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;4<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">1.95583</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;1.96<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">1241757</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">3</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;1242000<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">5.045</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;5.05<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">5.055</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;5.06<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3383">
    <p><strong>Example #2 <em><code class="parameter">mode</code></em> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">9.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_ROUND_HALF_UP</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;10<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">9.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_ROUND_HALF_DOWN</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;9<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">9.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_ROUND_HALF_EVEN</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;10<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">9.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_ROUND_HALF_ODD</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;9<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">8.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_ROUND_HALF_UP</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;9<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">8.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_ROUND_HALF_DOWN</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;8<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">8.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_ROUND_HALF_EVEN</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;8<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">8.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_ROUND_HALF_ODD</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;9<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.round-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        引入了 <em><code class="parameter">mode</code></em> 参数
       </td>
      </tr>

      <tr>
       <td>5.2.7</td>
       <td>
         <span class="function"><strong>round()</strong></span> 的内部运作修改符合 C99 的标准。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.round-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.ceil.html" class="function" rel="rdfs-seeAlso">ceil()</a> - 进一法取整</span></li>
    <li class="member"><span class="function"><a href="function.floor.html" class="function" rel="rdfs-seeAlso">floor()</a> - 舍去法取整</span></li>
    <li class="member"><span class="function"><a href="function.number-format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - 以千位分隔符方式格式化一个数字</span></li>
   </ul>
  </p>
 </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.rand.html">rand</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.sin.html">sin</a></div>
 <div class="up"><a href="ref.math.html">Math 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
